<template>
  <div class="app-container">
    <ContentWrap>
      <el-form
        v-show="showSearch"
        ref="queryRef"
        :model="queryParams"
        :inline="true"
      >
        <el-form-item label="加油站" prop="jyzId">
          <el-input
            v-model="queryParams.jyzId"
            placeholder="请输入加油站"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="时间" prop="shijian">
          <el-date-picker
            v-model="queryParams.shijian"
            clearable
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择时间"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">
            搜索
          </el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <ContentWrap>
      <el-table
        v-loading="loading"
        :data="zxsjList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="加油站" align="center" prop="jyzmc">
          <template #default="scope">
            <el-button
              type="primary"
              link
              @click="handleView(scope.row.jyzId, scope.row.jyzmc)"
            >
              {{ scope.row.jyzmc }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="时间" align="center" prop="shijian" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.shijian, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column label="罐压" align="center" prop="guanya" />
        <el-table-column
          label="油气处理装置排气浓度"
          align="center"
          prop="yqclzzpqnd"
        />
        <el-table-column label="卸油区油气浓度" align="center" prop="xyqyqnd" />
        <el-table-column
          label="人工量油井油气浓度"
          align="center"
          prop="rglyjyqnd"
        />
        <el-table-column label="加油区油气浓度" align="center" prop="jyqyqnd" />
      </el-table>

      <pagination
        v-show="total > 0"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        :total="total"
        @pagination="getList"
      />
    </ContentWrap>
    <!-- 添加或修改在线数据对话框 -->
    <el-dialog
      v-model="open"
      :title="title"
      width="1000px"
      append-to-body
      destroy-on-close
      @closed="
        () => {
          activeName = 'jbxx';
        }
      "
    >
      <el-tabs v-model="activeName" class="demo-tabs">
        <el-tab-pane label="基本信息" name="jbxx">
          <jbxx :id="curId"></jbxx>
        </el-tab-pane>
        <el-tab-pane label="实时数据" name="sssj" lazy>
          <sssj :id="curId"></sssj>
        </el-tab-pane>
        <el-tab-pane label="三次数据" name="scsj" lazy>
          <scsj :id="curId"></scsj>
        </el-tab-pane>
        <el-tab-pane label="在线数据" name="zxsj" lazy>
          <zxsj :id="curId"></zxsj>
        </el-tab-pane>
        <el-tab-pane label="气液比数据" name="qybsj" lazy>
          <qybsj :id="curId"></qybsj>
        </el-tab-pane>
        <!-- <el-tab-pane label="实时视频" name="sssp" lazy>
          <sssp :id="curId"></sssp>
        </el-tab-pane> -->
      </el-tabs>
    </el-dialog>
  </div>
</template>

<script setup name="Zxsj">
import {
  listZxsj,
  getZxsj,
  delZxsj,
  addZxsj,
  updateZxsj,
} from "@/api/yqhs/zxsj";
import { ref } from "vue";
import jbxx from "./components/jbxx.vue";
import sssj from "./components/sssj.vue";
import scsj from "./components/scsj.vue";
import zxsj from "./components/zxsj.vue";
import qybsj from "./components/qybsj.vue";
import sssp from "./components/sssp.vue";

//获取最新罐压
//获取最新进气口压
//获取加油枪实时数据

const activeName = ref("jbxx");
let curId = ref(null);

const { proxy } = getCurrentInstance();

const zxsjList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    jyzId: null,
    shijian: null,
    guanya: null,
    yqclzzpqnd: null,
    xyqyqnd: null,
    rglyjyqnd: null,
    jyqyqnd: null,
    rekark: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询在线数据列表 */
function getList() {
  loading.value = true;
  listZxsj(queryParams.value).then((response) => {
    zxsjList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    jyzId: null,
    shijian: null,
    guanya: null,
    yqclzzpqnd: null,
    xyqyqnd: null,
    rglyjyqnd: null,
    jyqyqnd: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    rekark: null,
  };
  proxy.resetForm("zxsjRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
function handleView(jyzId, jyzmc) {
  open.value = true;
  title.value = jyzmc;
  curId.value = jyzId;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加在线数据";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getZxsj(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改在线数据";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["zxsjRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateZxsj(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addZxsj(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除在线数据编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delZxsj(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "yqhs/zxsj/export",
    {
      ...queryParams.value,
    },
    `zxsj_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
